from Signature import *

class Boneh_Boyen(Signature):
    def __init__(self):
        super(Boneh_Boyen, self).__init__()

    def key_gen(self):
        x, y = self.group.random(ZR, 2)
        return {'pk': {'U': x * self.g, 'V': y * self.g}, 'sk': {'x': x, 'y': y}}

    def signature(self, m, sk):
        x, y = sk['x'], sk['y']
        m2zr = self.group.init(ZR, int.from_bytes(m, byteorder = 'big', signed = False))
        zero = self.group.init(ZR, 0)
        r = self.group.random(ZR)
        while x + m2zr + r * y == zero:
            r = self.group.random(ZR)
        return (1 / (x + m2zr + r * y) * self.g), r

    def verify(self, m, sig, pk):
        m2zr = self.group.init(ZR, int.from_bytes(m, byteorder = 'big', signed = False))
        W, r = sig
        return e(pk['U'] + m2zr * self.g + r * pk['V'], W) == e(self.g, self.g)

if __name__ == '__main__':
    bb = Boneh_Boyen()
    bb.test(b'hello world')
